﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;

namespace DAL
{
    public class TUKHOAQUANHUYENDAO : DAOBASE
    {
        public DataTable Search2(Search input)
        {
            DataTable final = new DataTable();
            String sql = "SELECT  DISTINCT MaQuanHuyen  FROM TUKHOAQUANHUYEN WHERE  INSTR('" + input.InPut + "', TuKhoaQuanHuyen) > 0";
            final = ExcuteQuery(sql);
            return final;
        }  
        public DataTable Search(Search input)
        {
            DataTable final = new DataTable();
            try
            {
                //thuc hien 
                String[] s = Until.ConvertStringToArray2(input.InPut);
                DataColumn[] cl = new DataColumn[] { new DataColumn("MaQuanHuyen") };
                final.Columns.AddRange(cl);
                for (int i = 0; i < s.Length; i++)
                {
                    String sql = "SELECT  DISTINCT MaQuanHuyen  FROM TUKHOAQUANHUYEN WHERE ";
                    int pos = 0;
                    if (i + 3 < s.Length)
                    {
                        pos += i + 3;
                    }
                    else
                    {
                        if (i + 3 == s.Length)
                        {
                            pos = s.Length - i;
                            if (pos < i)
                            {
                                pos = s.Length - 1;
                            }
                        }
                        if (i + 3 > s.Length)
                        {
                            pos = s.Length - 1;
                        }

                    }
                    sql += Until.CreateWhere2(s, "TuKhoaQuanHuyen", i, pos);
                  //  sql += Until.CreateWhere(s, "TuKhoaQuanHuyen");
                    DataTable dt = new DataTable();
                    dt = ExcuteQuery(sql);
                    if (dt.Rows.Count >= 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            Until.AddData(dr, ref final, "MaQuanHuyen", "MaQuanHuyen");
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return final;

        }

        //KHOA
        public DataTable Search3(Search input)
        {



            DataTable final = new DataTable();
            DataColumn[] cl = new DataColumn[] { new DataColumn("MaQuanHuyen") };
            final.Columns.AddRange(cl);

            //return "";

            string k = Until.ConvertStringToArraykhoa(input.InPut);
            k = k.ToLower();
            //for (int i = 0; i < sl / 100; i++)
            //{
            DataTable dttemp = ExcuteQuery("select * from TUKHOAQUANHUYEN");// limit " + (i * 100).ToString() + ",100");
            if (dttemp.Rows.Count >= 0)
            {
                foreach (DataRow dr in dttemp.Rows)
                {
                    string tdd = dr["tukhoaquanhuyen"].ToString();
                    tdd = tdd.ToLower();
                    if (k.Contains(tdd))
                    {
                        DataRow newrow = final.NewRow();
                        newrow["MaQuanHuyen"] = dr["MaQuanHuyen"].ToString();
                        final.Rows.Add(newrow);
                    }
                }
            }
            //}
            return final;
        }
    }
}
